{% extends "base.html" %}
{% load static %}

{% block title %}{{ account.name }}{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-12 col-md-10 col-lg-8">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <h1 class="h3 mb-0">{{ account.name }}</h1>
                    <p class="text-muted mb-0">
                        <i class="fas {% if account.type == 'personal' %}fa-user{% else %}fa-users{% endif %} me-1"></i>
                        {{ account.get_type_display }}
                        {% if account.family %}
                        · {{ account.family.name }}
                        {% endif %}
                    </p>
                </div>
                <div class="d-flex gap-2">
                    {% if can_edit %}
                    <a href="{% url 'bills:account-update' account.pk %}" class="btn btn-outline-primary">
                        <i class="fas fa-edit"></i> 编辑账本
                    </a>
                    {% endif %}
                    {% if is_owner %}
                    <a href="{% url 'bills:account-delete' account.pk %}" class="btn btn-outline-danger">
                        <i class="fas fa-trash-alt"></i> 删除账本
                    </a>
                    {% endif %}
                    <a href="{% url 'stats:home' account.pk %}" class="btn btn-outline-info">
                        <i class="fas fa-chart-line"></i> 统计分析
                    </a>
                </div>
            </div>

            <div class="row g-4 mb-4">
                <div class="col-md-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <h6 class="card-subtitle mb-2 text-muted">总收入</h6>
                            <h3 class="card-title text-success mb-0">{{ total_income|floatformat:2 }}</h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <h6 class="card-subtitle mb-2 text-muted">总支出</h6>
                            <h3 class="card-title text-danger mb-0">{{ total_expense|floatformat:2 }}</h3>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <h6 class="card-subtitle mb-2 text-muted">结余</h6>
                            <h3 class="card-title {% if balance >= 0 %}text-success{% else %}text-danger{% endif %} mb-0">
                                {{ balance|floatformat:2 }}
                            </h3>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header bg-transparent">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h5 class="mb-0">账单记录</h5>
                        <a href="{% url 'bills:bill-create' account.pk %}" class="btn btn-primary btn-sm">
                            <i class="fas fa-plus"></i> 记一笔
                        </a>
                    </div>
                    <form method="get" class="row g-3">
                        <div class="col-md-3">
                            {{ search_form.start_date.label_tag }}
                            {{ search_form.start_date }}
                        </div>
                        <div class="col-md-3">
                            {{ search_form.end_date.label_tag }}
                            {{ search_form.end_date }}
                        </div>
                        <div class="col-md-2">
                            {{ search_form.type.label_tag }}
                            {{ search_form.type }}
                        </div>
                        <div class="col-md-2">
                            {{ search_form.category.label_tag }}
                            {{ search_form.category }}
                        </div>
                        <div class="col-md-2">
                            {{ search_form.keyword.label_tag }}
                            {{ search_form.keyword }}
                        </div>
                        <div class="col-12 text-end">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> 搜索
                            </button>
                            <a href="{% url 'bills:account-detail' account.pk %}" class="btn btn-outline-secondary">
                                <i class="fas fa-redo"></i> 重置
                            </a>
                        </div>
                    </form>
                </div>
                <div class="list-group list-group-flush">
                    {% for bill in recent_bills %}
                    <div class="list-group-item">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <div class="d-flex align-items-center gap-2">
                                    <i class="{{ bill.category.icon }} {% if bill.type == 'income' %}text-success{% else %}text-danger{% endif %}"></i>
                                    <h6 class="mb-0">{{ bill.category.name }}</h6>
                                </div>
                                <small class="text-muted d-block">{{ bill.date|date:"SHORT_DATETIME_FORMAT" }}</small>
                                {% if bill.description %}
                                <small class="text-muted d-block">{{ bill.description }}</small>
                                {% endif %}
                            </div>
                            <div class="text-end">
                                <div class="{% if bill.type == 'income' %}text-success{% else %}text-danger{% endif %} mb-1">
                                    {{ bill.type|yesno:"+,-" }}{{ bill.amount|floatformat:2 }}
                                </div>
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'bills:bill-update' bill.pk %}" class="btn btn-outline-secondary btn-sm">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="{% url 'bills:bill-delete' bill.pk %}" class="btn btn-outline-danger btn-sm">
                                        <i class="fas fa-trash-alt"></i>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% empty %}
                    <div class="text-center py-5">
                        <img src="{% static 'images/empty.svg' %}" alt="没有账单" class="img-fluid mb-3" style="max-width: 200px;">
                        <p class="text-muted">还没有任何账单记录</p>
                    </div>
                    {% endfor %}
                </div>
                {% if recent_bills.paginator %}
                <div class="card-footer bg-transparent">
                    <nav aria-label="账单分页">
                        <ul class="pagination justify-content-center mb-0">
                            {% if recent_bills.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ recent_bills.previous_page_number }}{% if request.GET.start_date %}&start_date={{ request.GET.start_date }}{% endif %}{% if request.GET.end_date %}&end_date={{ request.GET.end_date }}{% endif %}{% if request.GET.type %}&type={{ request.GET.type }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.keyword %}&keyword={{ request.GET.keyword }}{% endif %}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            {% endif %}
                            
                            {% for num in recent_bills.paginator.page_range %}
                                {% if num == recent_bills.number %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                                {% elif num > recent_bills.number|add:'-3' and num < recent_bills.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}{% if request.GET.start_date %}&start_date={{ request.GET.start_date }}{% endif %}{% if request.GET.end_date %}&end_date={{ request.GET.end_date }}{% endif %}{% if request.GET.type %}&type={{ request.GET.type }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.keyword %}&keyword={{ request.GET.keyword }}{% endif %}">{{ num }}</a>
                                </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if recent_bills.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ recent_bills.next_page_number }}{% if request.GET.start_date %}&start_date={{ request.GET.start_date }}{% endif %}{% if request.GET.end_date %}&end_date={{ request.GET.end_date }}{% endif %}{% if request.GET.type %}&type={{ request.GET.type }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.keyword %}&keyword={{ request.GET.keyword }}{% endif %}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %} 